﻿using MediatR;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    /// <summary>
    /// 设备加酒 获取设备列表
    /// </summary>
    public class GetDeviceNameByAddWineQueries : IRequest<Json<List<GetDeviceNameByAddWineDTO>>>
    {

    }

    public class GetDeviceNameByAddWineQueriesHandler : IRequestHandler<GetDeviceNameByAddWineQueries, Json<List<GetDeviceNameByAddWineDTO>>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicetableRepository _idevicetableRepository;
        public GetDeviceNameByAddWineQueriesHandler(ICurrentUserService icurrentUserServices, IDevicetableRepository idevicetableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _idevicetableRepository = idevicetableRepository;
        }

        public async Task<Json<List<GetDeviceNameByAddWineDTO>>> Handle(GetDeviceNameByAddWineQueries request, CancellationToken cancellationToken)
        {
            List<GetDeviceNameByAddWineDTO> list = new List<GetDeviceNameByAddWineDTO>() { };
            int userid = _icurrentUserServices.GetUserId();
            if (userid <= 0)
            {
                return Json<List<GetDeviceNameByAddWineDTO>>.Failed("请先登录", list);
            }
            else
            {
                list = await _idevicetableRepository.GetDeviceListByAddWine(userid, cancellationToken);
                if (!(list != null && list.Any()))
                {
                    list = new List<GetDeviceNameByAddWineDTO>() { };
                }
                return Json<List<GetDeviceNameByAddWineDTO>>.Success("查询成功", list);
            }
        }
    }
}
